"""数据模型定义"""

from typing import Optional, List
from pydantic import BaseModel, Field
from datetime import datetime


class ProductInfo(BaseModel):
    """商品信息模型"""
    
    title: str = Field(..., description="商品标题")
    price: str = Field(..., description="商品价格")
    min_order: str = Field(default="", description="最小起订量")
    sales: str = Field(default="", description="销量信息")
    location: str = Field(default="", description="发货地")
    supplier: str = Field(default="", description="供应商名称")
    product_url: str = Field(..., description="商品链接")
    image_url: str = Field(default="", description="商品图片链接")
    rating: str = Field(default="", description="评分")
    review_count: str = Field(default="", description="评价数量")
    crawl_time: datetime = Field(default_factory=datetime.now, description="爬取时间")
    
    class Config:
        json_encoders = {
            datetime: lambda v: v.isoformat()
        }


class SearchResult(BaseModel):
    """搜索结果模型"""
    
    keyword: str = Field(..., description="搜索关键词")
    total_count: int = Field(default=0, description="总商品数量")
    products: List[ProductInfo] = Field(default_factory=list, description="商品列表")
    search_time: datetime = Field(default_factory=datetime.now, description="搜索时间")
    
    class Config:
        json_encoders = {
            datetime: lambda v: v.isoformat()
        }